package Tree_09.BinaryTree_01;

/*
 * @Author 罗
 * @date 2020/3/20 - 10:18 上午
 *
 */

import Tree_09.BinaryTree_01.DataStructure.BinaryTree;
import Tree_09.BinaryTree_01.Entity.HeroNode;

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.addOrderly(new HeroNode(70, "王"));

        binaryTree.addOrderly(new HeroNode(5, "应该在最左"));
        binaryTree.addOrderly(new HeroNode(10, "李"));
        binaryTree.addOrderly(new HeroNode(200, "罗"));
        binaryTree.addOrderly(new HeroNode(50, "牛"));
        binaryTree.addOrderly(new HeroNode(2, "乌拉"));
        binaryTree.addOrderly(new HeroNode(51, "牛"));

        boolean run = true;
        Scanner scanner = new Scanner(System.in);
        String key;
        int id;
        String name;
        while (run) {
            System.out.println("add:添加节点");
            System.out.println("find:查找节点");
            System.out.println("prefix:前序遍历");
            System.out.println("infix:中序遍历");
            System.out.println("suffix:后序遍历");
            System.out.println("exit:退出程序");
            System.out.println("preFind:前序查找");
            System.out.println("infixFind:中序查找");
            System.out.println("suffixFind:中序查找");
            System.out.println("del:删除树或者叶子节点");
            key = scanner.next();
            switch (key) {
                case "add": {
                    System.out.println("请输入要添加的节点的id");
                    id = scanner.nextInt();
                    System.out.println("请输入要添加节点的name");
                    name = scanner.next();
                    binaryTree.addOrderly(new HeroNode(id, name));
                }
                break;
                case "find": {
                    System.out.println("请输入要查找节点的id");
                    id = scanner.nextInt();
                    System.out.println(binaryTree.findById(id));
                }
                break;
                case "prefix": {
                    binaryTree.prefixList();
                }
                break;
                case "infix": {
                    binaryTree.infixList();
                }
                break;
                case "suffix": {
                    binaryTree.suffixList();
                }
                break;
                case "exit": {
                    scanner.close();
                    run = false;
                }
                break;
                case "preFind": {
                    System.out.println("请输入要查找的id");
                    System.out.println(binaryTree.findByPrefix(scanner.nextInt()));
                }
                break;
                case "infixFind": {
                    System.out.println("请输入要查找的id");
                    System.out.println(binaryTree.findByInfix(scanner.nextInt()));
                }
                break;
                case "suffixFind": {
                    System.out.println("请输入要查找的id");
                    System.out.println(binaryTree.findBySuffix(scanner.nextInt()));
                }
                break;
                case "del": {
                    System.out.println("请输入要删除的节点id");
                    HeroNode father = binaryTree.delete(scanner.nextInt());
                    if (father == null) {
                        System.err.println("删除失败");
                    } else {
                        System.out.println("删除成功，被删除节点的父节点是" + father);
                    }
                }
                break;
            }
        }
    }
}
